package com.chatroomdemo.common.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Integer userId = (Integer) request.getSession().getAttribute("session_userId");
        String username = (String) request.getSession().getAttribute("session_username");

        if(userId == null || username ==null){
            //拦截
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
